/*
	jumper : Copyright 2011 Dale J Williams

	Licensed under the Apache License, Version 2.0 (the "License");
	you may not use this file except in compliance with the License.
	You may obtain a copy of the License at

	http://www.apache.org/licenses/LICENSE-2.0

	Unless required by applicable law or agreed to in writing, software
	distributed under the License is distributed on an "AS IS" BASIS,
	WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
	See the License for the specific language governing permissions and
	limitations under the License.
*/
package lads.jumper.entities {
	
	import flash.display.MovieClip;
	import lads.flad.scene.collision.CollisionBox;
	import lads.flad.scene.entities.Entity;
	import lads.jumper.controllers.Controller;
	import lads.jumper.JumpScene;
	import lads.jumper.pixmap.PixMap;
	import lads.jumper.states.StateMachine;

	public class Actor extends Entity {
		
		private var _direction:Number = 1;
		private var _state:StateMachine;
		private var _collision:CollisionBox;
		private var _controller:Controller;
		private var _map:PixMap;
		private var _clip:MovieClip;
		private var animLabel:String;
		
		public function Actor(name:String) {
			super(name);
			_state = new StateMachine();
			_collision = new CollisionBox();
		}
		
		public function awake():void {
			_map = JumpScene(scene).map;
		}
		
		public function update():void {
			if (controller) {
				controller.update();
				if (controller.move) {
					direction = controller.right ? 1 : -1;
				}
			}
			
			collision.updateCollision(map, x, y);
			state.update();
			
			if (!clip) return;
			clip.scaleX = direction;
			if (clip.currentLabel != animLabel)
				clip.gotoAndPlay(animLabel);	
		}
		
		public function changeAnimation(label:String):void {
			animLabel = label;
		}
		
		public function get direction():Number { return _direction; }		
		public function set direction(value:Number):void {
			_direction = value >= 0 ? 1 : -1;
		}
		
		public function get isLeft():Boolean { return _direction < 0; }
		public function get isRight():Boolean { return _direction >= 0; }
		
		public function get state():StateMachine { return _state; }
		
		public function get collision():CollisionBox { return _collision; }
		
		public function get map():PixMap { return _map; }
		
		public function get controller():Controller { return _controller; }		
		public function set controller(value:Controller):void {
			_controller = value;
		}
		
		public function get clip():MovieClip { return _clip; }		
		public function set clip(value:MovieClip):void {
			if (_clip) removeChild(_clip);
			_clip = value;
			addChild(_clip);
		}
		
	}

}